<?php

    /**
     * Abstract
     *
     * @package      Core
     * @subpackage   Storage
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Storage_Cookie
    {

    ////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Instance
         *
         * @var     Core_Storage_Cookie
         */
        protected static $_instance = null;

        /**
         * Default domain
         *
         * @var     string
         */
        private $_domain = null;

        /**
         * Names of modified cookies
         *
         * @var     array
         */
        private $_newCookies = array();

	////////////////////////////////////////////////////////////////////////////
	//                                Methods                                 //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Returns single instance
         *
         * @return Core_Storage_Cookie
         */
        public static function getInstance()
        {
            if (self::$_instance === null) {
                self::$_instance = new Core_Storage_Cookie();
            }

            return self::$_instance;
        }

        /**
         * Constructor
         */
        private function __construct()
        {
            $this->_domain = Core_Url::getDomain();
        }

        /**
         * Destructor
         */
        public function __destruct()
        {
            unset($this);
        }

        /**
         * Creates or modify a cookie
         * 
         * @param   string  $name
         * @param   mixed   $value
         * @param   int     $expires
         * @param   string  $domain
         * @param   string  $path
         */
        public function addCookie($name, $value, $expires = null, $domain = null , $path = null)
        {
            if ($name === null) {
				throw new Core_ArgumentNullException('name is a null reference');
			}
            
            $registry = Zend_Registry::getInstance();

            if ($path === null) {
                $path = $registry['config']->urlengine->basePath;
            }

            $this->_newCookies[$name] = array(
                                                'name'      => $name,
                                                'value'     => $value,
                                                'domain'    => (($domain === null)     ? $this->_domain    : $domain),
                                                'expires'   => (($expires === null)    ? time() + 2592000  : $expires),
                                                'path'      => $path
                                             );
        }

        /**
         *
         * @param   string      $name
         * @return  string
         */
        public function getCookie($name)
        {
            if ($name === null) {
				throw new Core_ArgumentNullException('name is a null reference');
			}

            if (array_key_exists($name, $_COOKIE)) {
                return $_COOKIE[$name];
            }

            return null;
        }

        /**
         * Returns matching cookies as a string
         * 
         * @return      void
         */
        public function getMatchingCookies()
        {
            foreach ($this->_newCookies as $newCookie) {
                $domain = ($this->_domain != 'localhost') ? $this->_domain : false;
                setcookie($newCookie['name'], urlencode($newCookie['value']), ($newCookie['expires'] !== null) ? $newCookie['expires'] : 0, (($newCookie['path'] !== null) ? $newCookie['path'] : '/'), $domain);
            }
        }

    }
